package com.bbs.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.bbs.bean.BBSOfPCAuto;
import com.car.util.ConnectionPool;
import com.car.util.Util;

public class BBSDaoOfPCAuto {
	private Connection conn = ConnectionPool.getConnection("pcauto", "root", "zhouzhou100428", "192.168.1.168");
	private Connection co = ConnectionPool.getConnection("carvoice_core", "root", "zhouzhou100428", "192.168.1.168");

	// 得到BBSID
	public List<Integer> getBBSID() {
		List<Integer> list = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT pcauto_bbsid FROM core_brand_pcauto WHERE pcauto_bbsid>0";
		try {
			st = co.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getInt("pcauto_bbsid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void insertBBS(BBSOfPCAuto bbs) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO pcauto_bbs_" + bbs.getBbs_ppid()
				+ "(bbs_id,bbs_ppid,bbs_bt,bbs_sfjh,bbs_sfwd,bbs_fbsj,bbs_nr,bbs_hfs,bbs_yhid,bbs_yhmc,bbs_zc,bbs_tx,bbs_jf,bbs_zt,bbs_ht,bbs_jh) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, bbs.getBbs_id());
			ps.setInt(2, bbs.getBbs_ppid());
			ps.setString(3, bbs.getBbs_bt());
			ps.setInt(4, bbs.getBbs_sfjh());
			ps.setInt(5, bbs.getBbs_sfwd());
			ps.setDate(6, (Date) Util.getDate(bbs.getBbs_fbsj()));
			ps.setString(7, bbs.getBbs_nr());
			ps.setInt(8, bbs.getBbs_hfs());
			ps.setInt(9, bbs.getBbs_yhid());
			ps.setString(10, bbs.getBbs_yhmc());
			ps.setDate(11, (Date) Util.getDate(bbs.getBbs_zc()));
			ps.setString(12, bbs.getBbs_tx());
			ps.setInt(13, bbs.getBbs_jf());
			ps.setInt(14, bbs.getBbs_zt());
			ps.setInt(15, bbs.getBbs_ht());
			ps.setInt(16, bbs.getBbs_jh());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void createTable(int bbsid) {
		Statement st = null;
		String sql = "DROP TABLE IF EXISTS pcauto_bbs_" + bbsid;
		String sql2 = "create table pcauto_bbs_" + bbsid + " LIKE pcauto_bbs";
		try {
			st = conn.createStatement();
			st.executeUpdate(sql);
			st.executeUpdate(sql2);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateBBSTime(String type, int id) {
		PreparedStatement ps = null;
		String sql = "UPDATE core_brand_pcauto AS pc SET pc." + type + "=NOW() WHERE pc.pcauto_bbsid=" + id;
		try {
			ps = co.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
